//
//  RecordView.m
//  MoneyLog
//
//  Created by Max on 11-3-1.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "RecordView.h"
#import "RecordWrapper.h"
#import "GlobalDef.h"

@implementation RecordView

@synthesize recordWrapper;
@synthesize dateFormatter;
@synthesize highlighted;
@synthesize editing;

- (id)initWithFrame:(CGRect)frame {
    
    if (self == [super initWithFrame:frame]) {
        // Initialization code.
		dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setLocale:[NSLocale currentLocale]];
		self.opaque = YES;
		self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code.
}
*/

- (void)setRecordWrapper:(RecordWrapper *)newRecordWrapper {
	
	// If the time zone wrapper changes, update the date formatter and abbreviation string.
	if (recordWrapper != newRecordWrapper) {
		[recordWrapper release];
		recordWrapper = [newRecordWrapper retain];
		
		//[dateFormatter setTimeZone:recordWrapper.timeZone];
		
		//NSString *string = [[NSString alloc] initWithFormat:@"%@ (%@)", timeZoneWrapper.abbreviation, timeZoneWrapper.gmtOffset];
		//self.abbreviation = string;
		//[string release];
	}
	// May be the same wrapper, but the date may have changed, so mark for redisplay.
	[self setNeedsDisplay];
}

- (void)setHighlighted:(BOOL)lit {
	// If highlighted state changes, need to redisplay.
	if (highlighted != lit) {
		highlighted = lit;	
		[self setNeedsDisplay];
	}
}

- (void)drawRect:(CGRect)rect {
	// Color and font for the main text items (time zone name, time)
	UIColor *mainTextColor = nil;
	UIFont *mainFont = [UIFont systemFontOfSize:MAIN_FONT_SIZE];
	
	// Color and font for the secondary text items (GMT offset, day)
	UIColor *secondaryTextColor = nil;
	UIFont *secondaryFont = [UIFont systemFontOfSize:SECONDARY_FONT_SIZE];
	
	// Choose font color based on highlighted state.
	if (self.highlighted) {
		mainTextColor = [UIColor whiteColor];
		secondaryTextColor = [UIColor whiteColor];
	}
	else {
		mainTextColor = [UIColor blackColor];
		secondaryTextColor = [UIColor darkGrayColor];
		self.backgroundColor = [UIColor whiteColor];
	}
	
	CGRect contentRect = self.bounds;
	
	// In this example we will never be editing, but this illustrates the appropriate pattern.
    if (!self.editing) {
		
		CGFloat boundsX = contentRect.origin.x;
		CGPoint point;
		
		CGFloat actualFontSize;
		CGSize size;
		
		// Set the color for the main text items.
		[mainTextColor set];
		
		//类别
		point = CGPointMake(boundsX + LEFT_COLUMN_OFFSET, UPPER_ROW_TOP);
		[recordWrapper.kind drawAtPoint:point 
							   forWidth:LEFT_COLUMN_WIDTH 
							   withFont:mainFont 
							   minFontSize:MIN_MAIN_FONT_SIZE 
							   actualFontSize:NULL 
							   lineBreakMode:UILineBreakModeTailTruncation 
						       baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
		
		//金额 
		NSString *textString = [NSString stringWithFormat:@"%.0f",recordWrapper.money];
		size = [textString sizeWithFont:mainFont 
						   minFontSize:MIN_MAIN_FONT_SIZE 
						   actualFontSize:&actualFontSize 
						   forWidth:MIDDLE_COLUMN_WIDTH 
						   lineBreakMode:UILineBreakModeTailTruncation];
		
		point = CGPointMake(boundsX + MIDDLE_COLUMN_OFFSET + MIDDLE_COLUMN_WIDTH - size.width, UPPER_ROW_TOP);
		[textString drawAtPoint:point 
					forWidth:MIDDLE_COLUMN_WIDTH 
					withFont:mainFont 
					minFontSize:actualFontSize 
				    actualFontSize:&actualFontSize 
				    lineBreakMode:UILineBreakModeTailTruncation 
			        baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
		
		//时间
		[dateFormatter setDateFormat:@"hh:mm a"];
		textString = [dateFormatter stringFromDate:recordWrapper.date];
		size = [textString sizeWithFont:mainFont 
						   minFontSize:MIN_MAIN_FONT_SIZE 
						   actualFontSize:&actualFontSize 
						   forWidth:MIDDLE_COLUMN_WIDTH 
						   lineBreakMode:UILineBreakModeTailTruncation];
		
		point = CGPointMake(boundsX + RIGHT_COLUMN_OFFSET + RIGHT_COLUMN_WIDTH - size.width, UPPER_ROW_TOP);
		[textString drawAtPoint:point 
					forWidth:RIGHT_COLUMN_WIDTH 
					withFont:mainFont 
					minFontSize:actualFontSize 
				    actualFontSize:&actualFontSize 
				    lineBreakMode:UILineBreakModeTailTruncation 
			        baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
		
		// Set the color for the secondary text items.
		[secondaryTextColor set];
		
		//子类别
        if (recordWrapper.subKind == nil || [recordWrapper.subKind length] == 0) {
            recordWrapper.subKind = @"收入";
        }
		point = CGPointMake(boundsX + LEFT_COLUMN_OFFSET, LOWER_ROW_TOP);
		[recordWrapper.subKind drawAtPoint:point 
							forWidth:LEFT_COLUMN_WIDTH 
							withFont:secondaryFont 
							minFontSize:MIN_SECONDARY_FONT_SIZE 
						    actualFontSize:NULL 
						    lineBreakMode:UILineBreakModeTailTruncation 
					        baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
		
		//帐户 
		size = [recordWrapper.account sizeWithFont:secondaryFont 
							minFontSize:MIN_SECONDARY_FONT_SIZE 
						 actualFontSize:&actualFontSize 
							   forWidth:MIDDLE_COLUMN_WIDTH 
						  lineBreakMode:UILineBreakModeTailTruncation];
		
		point = CGPointMake(boundsX + MIDDLE_COLUMN_OFFSET + MIDDLE_COLUMN_WIDTH - size.width, LOWER_ROW_TOP);
		[recordWrapper.account drawAtPoint:point 
					   forWidth:MIDDLE_COLUMN_WIDTH 
					   withFont:secondaryFont 
					minFontSize:actualFontSize 
				 actualFontSize:&actualFontSize 
				  lineBreakMode:UILineBreakModeTailTruncation 
			 baselineAdjustment:UIBaselineAdjustmentAlignBaselines];		
	
		//日期
		[dateFormatter setDateFormat:@"yyyy-MM-dd"];
		textString = [dateFormatter stringFromDate:recordWrapper.date];
		size = [textString sizeWithFont:secondaryFont 
						   minFontSize:MIN_SECONDARY_FONT_SIZE 
						   actualFontSize:&actualFontSize 
						   forWidth:MIDDLE_COLUMN_WIDTH 
						   lineBreakMode:UILineBreakModeTailTruncation];

		point = CGPointMake(boundsX + RIGHT_COLUMN_OFFSET + RIGHT_COLUMN_WIDTH - size.width, LOWER_ROW_TOP);
		[textString drawAtPoint:point 
								forWidth:RIGHT_COLUMN_WIDTH 
								withFont:secondaryFont 
								minFontSize:actualFontSize 
								actualFontSize:&actualFontSize 
								lineBreakMode:UILineBreakModeTailTruncation 
								baselineAdjustment:UIBaselineAdjustmentAlignBaselines];		
	}
}

- (void)dealloc {
	[recordWrapper release];
	[dateFormatter release]; 
    [super dealloc];
}


@end
